package org.casper.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.casper.bean.Board;
import org.casper.service.BoardService;

/**
 * 跳转到用户首页的servlet
 */
public class UserIndexServlet extends HttpServlet {
	HttpSession session = null;
	private static final long serialVersionUID = 1L;
	private BoardService boardService = new BoardService();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		session = req.getSession();
		/**
		 * 获取版块菜单
		 * 
		 * 以及是否记住密码登录
		 * 
		 */
		
		HashMap<Board, ArrayList<Board>> bmap = new HashMap<Board, ArrayList<Board>>();
		
		ArrayList<Board> boards = boardService.selectAllBoards();
		
		ArrayList<Board> pboards = new ArrayList<Board>();
		
		/**
		 * 先保存的所有的付版块 作为key
		 */
		for (Board board : boards) {
			if(board.getPbid() == 0){
				pboards.add(board);
			}
		}
		
		for(Board pb: pboards){
			ArrayList<Board> sboards = null;
			for(Board b:boards){
				if(pb.getId() == b.getPbid()){
					sboards = new ArrayList<Board>();
					sboards.add(b);
				}
				bmap.put(pb, sboards);
			}
		}
		
		
		session.setAttribute("bmap", bmap);
		resp.sendRedirect("user-index.jsp");
	
	}
	

}
